/*
 * Copyright (c) 2016-2019 Michael Zhang <yidongnan@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package keter.lab.grpc.cloud.server;

import com.google.protobuf.Descriptors;
import io.grpc.stub.StreamObserver;
import keter.lab.grpc.cloud.PerfData;
import net.devh.boot.grpc.examples.lib.HelloReply;
import net.devh.boot.grpc.examples.lib.HelloRequest;
import net.devh.boot.grpc.examples.lib.Person;
import net.devh.boot.grpc.examples.lib.SimpleGrpc;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Michael (yidongnan@gmail.com)
 * @since 2016/11/8
 */

@GrpcService
public class GrpcServerService extends SimpleGrpc.SimpleImplBase {

    @Override
    public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {

        Person person1 = getPerson("person");
        HelloReply reply = HelloReply.newBuilder()
                .setMessage("Hello ==> " + req.getName())
                .setCount(1)
                .addPerson(person1)
                .build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }

    @Override
    public void sayRecursion(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
        Person person1 = getPerson("person");
        Person person2 = getPerson("person2",person1);
        Person person3 = getPerson("person3",person2);
        Person person4 = getPerson("person4",person3);
        Person person5 = getPerson("person5",person4);
        Person person6 = getPerson("person6",person5);
        HelloReply reply = HelloReply.newBuilder()
                .setMessage("Hello ==> " + req.getName())
                .setCount(1)
                .addPerson(person6)
                .build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }

    private Person getPerson(String name){
        Person person = Person.newBuilder()
                .setName(name)
                .setAge(1)
                .build();
        return person;
    }

    private Person getPerson(String name,Person p){
        Person person = Person.newBuilder()
                .setName(name)
                .setAge(1)
                .addPerson(p)
                .build();
        return person;
    }

    @Autowired
    PerfData data;

    /**
     * 大JSON字符串
     * @param req
     * @param responseObserver
     */
    @Override
    public void sayBigJsonString(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
            HelloReply reply = HelloReply.newBuilder().setMessage(data.bigJsonString()).build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }
}
